from pathlib import Path
from openai import OpenAI
import os, tempfile

os.environ["OPENAI_API_KEY"] = "sk-fbN36d8rqnS5c7CK303f24723a1f4751B0E794318745Ef6a"
os.environ["OPENAI_API_BASE"] = "https://ai-yyds.com/v1"

client = OpenAI(api_key="sk-fbN36d8rqnS5c7CK303f24723a1f4751B0E794318745Ef6a",
                base_url="https://ai-yyds.com/v1", timeout=120)

speech_file_path = Path(__file__).parent / "speech.mp3"
print(speech_file_path)
response = client.audio.speech.create(
  model="tts-1-1106",
  voice="alloy",
  input="你好啊哈哈"
)

# with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_file:
#   temp_file.write(response.content)
#
# print(temp_file.name)

# response.stream_to_file(speech_file_path)

with open(speech_file_path, 'wb') as file:
    file.write(response.content)